home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with c code, please help!
- Date: 22 Jan 1996 17:32:16 GMT
- Organization: OpenVision
- Message-ID: <4e0hn0$e80@spanky.pls.ov.com>
- References: <surgsw-1901960148530001@128.206.206.86>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article 1901960148530001@128.206.206.86, surgsw@mizzou1.missouri.edu (Joel Weinstein) writes:
- >I have been trying to get this very simple piece of code to work for
- >hours. What is the problem???????
- >
- >#include <stdio.h>
- >
- >main()
- >{
- > int i=0;
- > char word[100], c;
- > printf("Enter a word: ");
- > while( (c = getchar()) != '\n') {
- > *word = c;
- ^^^^^
- Problem #1, word is not a pointer. You can't dereference it. If you
- wish to use a pointer into the "word" array, you need to create and
- initialize the pointer.
-
- > word == word + 1;
- ^^
- Problem #2, this is a test for equality. In this case it will always
- test false, and does not affect the value in "word" (which is not a pointer).
-
- > }
- > printf("You entered: ");
- > *word = 0;
- > while( *word != '\n' )
- > {
- > putchar( *word );
- > word == word + 1;
- > }
- >}
- >
- >I think the problem I am having is due to my not knowing when to use the
- >'*' operator. When do you use it? Also, why won't my goddam compiler let
- >me do word++; instead of word == word +1; I also tried to do *word++;
- >and it didn't work, what is the deal with that. It won't let me put word
- >= word + 1;. It insists on the double ='s. Why is that?
- >
- >Please send e-mail.
- >
- >Joel
- >
- >ps: is there a way to find out what exactly error messages mean? I kept
- >getting something similar to: not an Ivalue. It would be nice if I knew
- >what the hell that meant.
-
- A corrected version of your code is below:
-
- #include <stdio.h>
-
- main()
- {
- int i=0, c; /* getchar() returns an int */
- char word[100], *ptr; /* Defining a pointer here */
- ptr = word; /* point to the first character of "word" */
- printf("Enter a word: ");
- while( (c = getchar()) != '\n') {
- *ptr = (char) c; /* put the data into the array */
- ptr++; /* point to the next location to be filled */
- }
- *ptr = '\0'; /* terminate the string */
- printf("You entered: %s\n", word);
- }
-
- Fletcher.Glenn@ov.com
-
-
-